const app = getApp()
import mode from '../../utils/mode.js'
Page({
    /**
     * 页面的初始数据
     */
    data: {
        isLoad: false,
        id: '',
        type: 1,//1新增  2编辑  3修改订单收货地址
        order_id: '',//订单id
        orders_type: '',//订单类型
        address_id: '',
        loading: false,
        region: '',
        name: '',
        phone: '',
        address: '',
        addressdetail: '',
        isDefault: false
    },
    formSubmit (e) {

        this.setData({
            loading: !this.data.loading
        })
        setTimeout(() => {
            this.setData({
                loading: false
            })
        }, 2000)
        console.log(e.detail.value)
    },
    // bindRegionChange: function (e) {
    //     console.log('picker发送选择改变，携带值为', e.detail.value)
    //     this.setData({
    //       region: e.detail.value
    //     })
    // },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var uinfo = wx.getStorageSync('uinfo')
        this.setData({
            id: uinfo.user.id
        })
        if (options.type) {
            this.setData({
                type: options.type,
                orders_type: options.orders_type,
                order_id: options.order_id
            })
        }
        if (options.type == 3) {
            this.getOrdersInfo()
        }
        if (options.address_id) {
            this.getAddress(options.address_id)
        }
    },
    async getOrdersInfo (type = true) {//查询订单地址
        if (type) wx.showLoading()
        let { code, data } = await mode.getOrdersInfo({
            orders_type: this.data.orders_type,
            user_id: this.data.id,
            id: this.data.order_id
        })
        if (type) wx.hideLoading()
        if (code == 1) {
            if (!data.address) wx.showToast({ title: '订单地址为空!', icon: 'none' })
            let { name, phone, address, addressdetail, id } = data.address
            this.setData({
                region: address.split(','),
                address_id: id,
                name,
                phone,
                address,
                addressdetail,
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    myname (e) {
        // console.log(e.detail.value)
        this.setData({
            name: e.detail.value
        })
    },
    myphone (e) {
        let { value } = e.detail
        let num = ''
        num = value.replace(/\D/g, '')
        this.setData({
            phone: num
        })
    },
    bindRegionChange (e) {
        let str = e.detail.value
        this.setData({
            region: e.detail.value,
            address: str.join(','),
        })
    },
    mydetailaddress (e) {
        // console.log('1', e.detail.value)
        this.setData({
            addressdetail: e.detail.value
        })
    },
    switch1Change (e) {
        // console.log('1', e.detail.value)
        this.setData({
            isDefault: e.detail.value
        })
    },
    async tosave () {
        var that = this
        if (that.data.isLoad) return
        that.setData({
            isLoad: true
        })
        if (that.data.name == '' || that.data.phone == '' || that.data.address == '' || that.data.addressdetail == '') {
            wx.showModal({
                title: '提示',
                content: '请将信息填写完整',
                showCancel: false
            })
            that.setData({
                isLoad: false
            })
        } else {
            //修改订单收货地址
            if (this.data.type == 3) {
                let { address_id, name, phone, address, addressdetail } = this.data
                wx.showLoading()
                let { code, data } = await mode.saveOrderAddress({
                    id: address_id,
                    name,
                    phone,
                    address,
                    addressdetail
                })
                wx.hideLoading()
                if (code == 1) {
                    app.backPage()
                    wx.showToast({ title: '地址修改成功!', icon: 'none' })
                }
                return
            }
            // let phone = /^[1]([3-9])[0-9]{9}$/
            // if (!phone.test(that.data.phone)) {
            //     wx.showToast({
            //         title: '手机号码不正确！',
            //         icon: 'none'
            //     })
            // } else {

            // }
            let postData= {
                name: that.data.name,
                phone: that.data.phone,
                addressdetail: that.data.addressdetail,
                address: that.data.address,
                user_id: that.data.id,
                id: that.data.address_id,
                is_default: that.data.isDefault ? 1 : 0
            }
            if(that.data.type == 1){
                wx.$api.saveAddress(postData).then(res=>{
                    if (res.code == 1) {
                        wx.showToast({
                            title: '保存成功',
                            icon: 'success',
                            duration: 2000
                        })
                        that.setData({
                            isLoad: false
                        })
                        app.backPage()
                    } else {
                        that.setData({
                            isLoad: false
                        })
                        wx.showToast({
                            title: '保存失败',
                            icon: 'error',
                            duration: 2000
                        })
                    }
                })
            }else{
                wx.$api.editAddress(postData).then(res=>{
                    if (res.code == 1) {
                        wx.showToast({
                            title: '保存成功',
                            icon: 'success',
                            duration: 2000
                        })
                        that.setData({
                            isLoad: false
                        })
                        app.backPage()
                    } else {
                        that.setData({
                            isLoad: false
                        })
                        wx.showToast({
                            title: '保存失败',
                            icon: 'error',
                            duration: 2000
                        })
                    }
                })
            } 
        }
    },
    // 删除地址
    deleAddress () {
        let that = this
        wx.showModal({
            title: '提示',
            content: '是否确认删除该地址',
            success: function (res) {
                if (res.confirm) {
                    wx.$api.delAddress({id: that.data.address_id
                        }).then(res=>{
                            if(res.code){
                                wx.showToast({
                                    title: '删除成功',
                                    icon: 'success',
                                    duration: 1000
                                })
                                app.backPage()
                            }else{
                                wx.showToast({
                                    title: '删除失败',
                                    icon: 'error',
                                    duration: 1000
                                })
                            }
                            
                    })
                }
            }
        })


    },
    getAddress (id) { //获取编辑地址详情
        var that = this
        wx.$api.getAddressInfo({
                id: id
            }).then(res=>{
                let { data,code } = res
                if (code == 1) {
                    that.setData({
                        region: data[0].address.split(',')
                    })
                    that.setData({
                        region: data[0].address.split(','),
                        name: data[0].name,
                        phone: data[0].phone,
                        address: data[0].address,
                        addressdetail: data[0].addressdetail,
                        address_id: data[0].id,
                        isDefault: data[0].is_default == 1 ? true : false,
                    })
                }
        })
    }
})